﻿using FluentValidation;
using Nuclear.Models.DtoModels;

namespace Nuclear.Validations.DtoValidations;
using static Statics;

public class UseConsumeCodeRequestDtoValidator: AbstractValidator<UseConsumeCodeRequestDto>
{
    public UseConsumeCodeRequestDtoValidator()
    {
        RuleFor(x => x.SystemId).MinimumLength(2)
            .WithErrorCode("System id must be at least 2 letters")
            .WithMessage("系统ID至少包含两个字符");
        RuleFor(x => x.MerchantId).NotEmpty()
            .WithErrorCode("Merchant ID must not be empty")
            .WithMessage("商户ID不能为空");
        RuleFor(x => x.ProductId).NotEmpty()
            .WithErrorCode("Product ID must not be empty")
            .WithMessage("产品ID不能为空");
        RuleFor(x => x.UserId)
            .Must(x => x.Length == ChinaMainlandPhoneNumberLength &&
                       x[0] == '1' &&
                       x.All(char.IsDigit))
            .WithErrorCode("User ID must be a China mainland mobile phone number")
            .WithMessage("用户ID必须是中国大陆手机号码");
        RuleFor(x => x.ConsumeCode)
            .Must(x => x.Length == ConsumeCodeLength &&
                       char.IsAsciiLetterUpper(x[0]) &&
                       char.IsAsciiLetterUpper(x[1]) &&
                       x.All(c => char.IsAsciiDigit(c) || char.IsAsciiLetter(c)))
            .WithErrorCode($"Consume code must be {ConsumeCodeLength} alphanumeric characters and start with two upper letters")
            .WithMessage($"消费码必须为{ConsumeCodeLength}个字母或数字, 前两位必须是大写字母");

        RuleFor(x => x.BenefitsCardNo)
            .Must(x =>
            {
                if (string.IsNullOrEmpty(x))
                    return true;

                return x.Length >= MinBenefitsCardNoLength &&
                       x[0] != '0' &&
                       x.All(char.IsAsciiDigit);
            })
            .WithErrorCode($"The benefits card NO. must be all digits and cannot be less than {MinBenefitsCardNoLength} digits")
            .WithMessage($"权益卡号必须全部是数字并至少{MinBenefitsCardNoLength}位");
    }
}